# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# 先序遍历 + 递归

class Solution:
    def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
        def is_sub(n1, n2):
            if not n2:
                return True
            if not n1:
                return False
            return n1.val == n2.val and is_sub(n1.left, n2.left) and is_sub(n1.right, n2.right)
    
        return bool(B and A) and (is_sub(A,B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B))
        